package com.gdth.bank.widget;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.bank.adapter.JtcyCpzlListAdapter;
import com.gdth.bank.bean.BfjlCpxqItem;
import com.gdth.bank.presenter.TAppBfrjJtcyCpxqPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.TAppBfrjJtcyCpxqView;
import com.gdth.database.bean.TAppBfrjCpxq;
import com.gdth.model.Item;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseDialog;

public class JtcyZcDialog extends BaseDialog implements TAppBfrjJtcyCpxqView{
	ListView mListView;
	private View.OnClickListener mOnClickListener;
	private TAppBfrjCpxq  mTAppBfrjCpxq;
	private JtcyCpzlListAdapter  adapter;
	private List<Item> items;
	private List<TAppBfrjCpxq> mTAppBfrjCpxqList,tTAppBfrjCpxqList,xTAppBfrjCpxqList;
	private List<BfjlCpxqItem>   mBfjlCpxqItemList,tBfjlCpxqItemList,xBfjlCpxqItemList;  //tBfjlCpxqItemList对比数据,xBfjlCpxqItemList  checkbox勾选数据
	private List<String>   keyList;
	TAppBfrjJtcyCpxqPresenter    mTAppBfrjJtcyCpxqPresenter;
	public JtcyZcDialog(Context context, String title, boolean dismiss) {
		super(context, R.style.dialog_theme);
		
		this.title = title;
		this.dismiss = dismiss;
		
		initView();
	}
	
	public JtcyZcDialog(Context context, String title, boolean dismiss, View.OnClickListener mOnClickListener) {
		super(context, R.style.dialog_theme);
		
		this.title = title;
		this.dismiss = dismiss;
		this.mOnClickListener = mOnClickListener;
		
	    items = AppUtil.getCanshuByType(context, AppUtil.SHARE_DATA_CPLX);
		
		init();
		initView();
	}
	
	protected void init(){
		mTAppBfrjJtcyCpxqPresenter= new TAppBfrjJtcyCpxqPresenter(this);
		mTAppBfrjCpxqList = new ArrayList<TAppBfrjCpxq>();
		mBfjlCpxqItemList = new ArrayList<BfjlCpxqItem>(); 
		xBfjlCpxqItemList = new ArrayList<BfjlCpxqItem>();
		xTAppBfrjCpxqList= new ArrayList<TAppBfrjCpxq>();
		keyList=new ArrayList<String>();
		for(int i=0;i<items.size();i++){
			/*TAppBfrjCpxq mTAppBfrjCpxq= new TAppBfrjCpxq();
			mTAppBfrjCpxq.setCpmc(items.get(i).getValue());
			mTAppBfrjCpxq.setCpzl(Integer.parseInt(items.get(i).getKey()));
			mTAppBfrjCpxqList.add(mTAppBfrjCpxq);*/
			BfjlCpxqItem  mBfjlCpxqItem = new BfjlCpxqItem();
			mBfjlCpxqItem.setKey(items.get(i).getKey());
			mBfjlCpxqItem.setValue(items.get(i).getValue());
			mBfjlCpxqItem.setCheck(false);
			mBfjlCpxqItem.setEdit(true);
			mBfjlCpxqItemList.add(mBfjlCpxqItem);
		}
		
	}
	
	
	protected void initView(){
		super.initView(R.layout.jtcyzc_dialog);
		
		LinearLayout linearlayout = (LinearLayout) findViewById(R.id.dialog_content);
		mListView = (ListView) findViewById(R.id.cjkh_jtxx_cpxq_listview);
		
		if (dismiss)
			linearlayout.setOnClickListener(new dismissOnClickListener());
        
		TextView txt_config = (TextView) findViewById(R.id.dialog_button_ok);
		
		TextView txt_cancle = (TextView) findViewById(R.id.dialog_button_cancel);
		txt_cancle.setOnClickListener(new dismissOnClickListener());
		
		if (this.mOnClickListener != null)
			txt_config.setOnClickListener(this.mOnClickListener);
		else 
			txt_config.setOnClickListener(new configOnClickListener());
		adapter = new JtcyCpzlListAdapter(context, mBfjlCpxqItemList);
		 mListView.setAdapter(adapter);
		
	}
	
	private class configOnClickListener implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dismiss();
			
			if (configListener != null){
				configListener.OnConfigClickListener();
			}
		}
	}
	
	public List<TAppBfrjCpxq> getCpZlXx(String jtuuid,String jcuuid){
		int  dataSize=mBfjlCpxqItemList.size();
		List<TAppBfrjCpxq> mTAppBfrjCpxqData = new ArrayList<TAppBfrjCpxq>();
		
		
		for(int i=0;i<dataSize;i++){
			if(mBfjlCpxqItemList.get(i).isCheck())
				xBfjlCpxqItemList.add(mBfjlCpxqItemList.get(i));
		}
		/*if(tTAppBfrjCpxqList==null && dataSize>0){
			for(int i =0;i<dataSize;i++){
				if(!mBfjlCpxqItemList.get(i).isCheck())
					continue;
				TAppBfrjCpxq tAppBfrjCpxq = new TAppBfrjCpxq();
				tAppBfrjCpxq.setJtuuid(jtuuid);
				tAppBfrjCpxq.setCpzl(Integer.parseInt(mBfjlCpxqItemList.get(i).getKey()));
				tAppBfrjCpxq.setCpmc(mBfjlCpxqItemList.get(i).getValue());
				tAppBfrjCpxq.setCjsj(new Date());
				tAppBfrjCpxq.setJcuuid(jcuuid);
				String appuuid = UUIDUtil.getUUID();
				tAppBfrjCpxq.setUuid(appuuid);
				tAppBfrjCpxq.setSjzt(AppUtil.sjzt_create);
				tAppBfrjCpxq.setBbh(1);
				mTAppBfrjCpxqData.add(tAppBfrjCpxq);
			}
			
		}
		*/
		    
		if(tTAppBfrjCpxqList==null && dataSize>0){
			for (BfjlCpxqItem yxCpxqItem : xBfjlCpxqItemList) {
				TAppBfrjCpxq tAppBfrjCpxq = new TAppBfrjCpxq();
				tAppBfrjCpxq.setJtuuid(jtuuid);
				tAppBfrjCpxq.setCpzl(Integer.parseInt(yxCpxqItem.getKey()));
				tAppBfrjCpxq.setCpmc(yxCpxqItem.getValue());
				tAppBfrjCpxq.setCjsj(new Date());
				tAppBfrjCpxq.setJcuuid(jcuuid);
				String appuuid = UUIDUtil.getUUID();
				tAppBfrjCpxq.setUuid(appuuid);
				tAppBfrjCpxq.setSjzt(AppUtil.sjzt_create);
				tAppBfrjCpxq.setBbh(1);
				tAppBfrjCpxq.setYxdjzt(1);
				mTAppBfrjCpxqData.add(tAppBfrjCpxq);
			}
			
		}else{
			//已选记录与历史记录循环对比
			for (BfjlCpxqItem yxCpxqItem : xBfjlCpxqItemList) {
				//历史记录中不存在已选记录，则添加
				boolean tag = true;
				for (TAppBfrjCpxq lsCpxq : tTAppBfrjCpxqList) {
					if(lsCpxq.getCpzl() == Integer.valueOf(yxCpxqItem.getKey())){
						tag = false;
					}
				}
				
				if(tag){
					TAppBfrjCpxq tAppBfrjCpxq = new TAppBfrjCpxq();
					tAppBfrjCpxq.setJtuuid(jtuuid);
					tAppBfrjCpxq.setCpzl(Integer.parseInt(yxCpxqItem.getKey()));
					tAppBfrjCpxq.setCpmc(yxCpxqItem.getValue());
					tAppBfrjCpxq.setCjsj(new Date());
					tAppBfrjCpxq.setJcuuid(jcuuid);
					String appuuid = UUIDUtil.getUUID();
					tAppBfrjCpxq.setUuid(appuuid);
					tAppBfrjCpxq.setSjzt(AppUtil.sjzt_create);
					tAppBfrjCpxq.setBbh(1);
					tAppBfrjCpxq.setYxdjzt(1);
					mTAppBfrjCpxqData.add(tAppBfrjCpxq);
				}
				
			}
			
			for (TAppBfrjCpxq lsCpxq : tTAppBfrjCpxqList) {
				//历史记录不存在在已先记录，则修改
				boolean tag = true;
				for (BfjlCpxqItem yxCpxqItem : xBfjlCpxqItemList) {
					if(lsCpxq.getCpzl() == Integer.valueOf(yxCpxqItem.getKey())){
						tag = false;
					}
				}
				
				if(tag){
					mTAppBfrjJtcyCpxqPresenter.getjtcyxxCpxqId(lsCpxq.getId());
				}
			}
		
		
			
		
	}
		return  mTAppBfrjCpxqData;
	}
	
	
	public void  setCpZlXx(String jtuuid){
		mTAppBfrjJtcyCpxqPresenter.getjtcyxxCpxq(jtuuid);
		
	}

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void saveSuccess(List<TAppBfrjCpxq> mTAppBfrjCpxqList) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getjtcyCpxqXx(List<TAppBfrjCpxq> arg0) {
		// TODO Auto-generated method stub
		tTAppBfrjCpxqList=arg0;
		/*xBfjlCpxqItemList =	updateItremXx(arg0);*/
		
		if(arg0.size()>0){
			tBfjlCpxqItemList =	adapter.updateItrem(arg0);
		}
	}
	public List<BfjlCpxqItem> updateItremXx(List<TAppBfrjCpxq> arg0){
		for (TAppBfrjCpxq mTAppBfrjCpxq : arg0){
			for (BfjlCpxqItem item : mBfjlCpxqItemList) {
				if (mTAppBfrjCpxq.getCpzl() == Integer.valueOf(item.getKey().toString())){
					item.setCpid(mTAppBfrjCpxq.getId());
					item.setCheck(true);
				}
			}
			
		}
		
		return  mBfjlCpxqItemList;
	}
	
}

